package com.itheima.service.store.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.store.CompanyDao;
import com.itheima.domain.store.Company;
import com.itheima.factory.MapperFactory;
import com.itheima.service.store.CompanyService;
import com.itheima.utils.TransactionUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.UUID;

/**
 * @program: mm
 * @description:
 * @author: zhanghz001
 * @create: 2020-11-12 14:44
 **/
public class CompanyServiceImpl implements CompanyService {

    @Override
    public void save(Company company) {
        //加载mybatis的资源文件

        //获取sqlSessionFactory
        SqlSession sqlSession = null;
        try {   //获取sqlSession
            sqlSession = MapperFactory.getSqlSession();
            //获取Mapper接口
            CompanyDao companyDao = MapperFactory.getMapper(sqlSession, CompanyDao.class);

            company.setId(UUID.randomUUID().toString());
            // 执行方法
            companyDao.save(company);
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
        } finally {

            TransactionUtil.close(sqlSession);
        }
    }

    @Override
    public void delete(Company company) {
        //加载mybatis的资源文件

        //获取sqlSessionFactory
        SqlSession sqlSession = null;
        try {   //获取sqlSession
            sqlSession = MapperFactory.getSqlSession();
            //获取Mapper接口
            CompanyDao companyDao = MapperFactory.getMapper(sqlSession, CompanyDao.class);

            // 执行方法
            companyDao.delete(company);
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
        } finally {

            TransactionUtil.close(sqlSession);
        }
    }

    @Override
    public void update(Company company) {
        //加载mybatis的资源文件

        //获取sqlSessionFactory
        SqlSession sqlSession = null;
        try {   //获取sqlSession
            sqlSession = MapperFactory.getSqlSession();
            //获取Mapper接口
            CompanyDao companyDao = MapperFactory.getMapper(sqlSession, CompanyDao.class);

            // 执行方法
            companyDao.update(company);
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
        } finally {

            TransactionUtil.close(sqlSession);
        }
    }

    @Override
    public Company findById(String id) {
        //
        //加载mybatis的资源文件

        //获取sqlSessionFactory

        SqlSession sqlSession = null;
        try {   //获取sqlSession
            sqlSession = MapperFactory.getSqlSession();
            //获取Mapper接口
            CompanyDao companyDao = MapperFactory.getMapper(sqlSession, CompanyDao.class);
            // 执行方法
            Company company = companyDao.findById(id);
            TransactionUtil.commit(sqlSession);
            return company;
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
        } finally {

            TransactionUtil.close(sqlSession);
        }
    }

    @Override
    public List<Company> findAll() {
        //
        //加载mybatis的资源文件

        //获取sqlSessionFactory

        SqlSession sqlSession = null;
        try {   //获取sqlSession
            sqlSession = MapperFactory.getSqlSession();
            //获取Mapper接口
            CompanyDao companyDao = MapperFactory.getMapper(sqlSession, CompanyDao.class);
            // 执行方法
            List<Company> list = companyDao.findAll();
            TransactionUtil.commit(sqlSession);
            return list;
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
        } finally {

            TransactionUtil.close(sqlSession);
        }
    }

    @Override
    public PageInfo findAll(int page, int size) {
        SqlSession sqlSession = null;
        try {   //获取sqlSession
            sqlSession = MapperFactory.getSqlSession();
            //获取Mapper接口
            CompanyDao companyDao = MapperFactory.getMapper(sqlSession, CompanyDao.class);
            // 执行方法
            //
            PageHelper.startPage(page, size);
            //
            List<Company> list = companyDao.findAll();
            //
            PageInfo<Company> pageInfo = new PageInfo<>(list);
            TransactionUtil.commit(sqlSession);
            return pageInfo;
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
        } finally {

            TransactionUtil.close(sqlSession);
        }
    }
}
